package com.xfcycle.zebrabikegp.util;

import android.util.Log;

import com.xfcycle.zebrabikegp.listener.BleInstructQueueLooperListener;

import java.util.Arrays;
import java.util.Stack;

/**
 * 蓝牙指令存储队列工具类
 * Created by xFcycle on 2017/5/25.
 */

public class BluetoothInstructStackUtil {

    private static final String TAG = "BluetoothInstructStack";

    private static boolean queueLooperState = true;

    private BleInstructQueueLooperListener mbleInstructQueueLooperListener;

    private static InstructLooper mInstructLooper;

    private static Stack<String> instructQueue;

    private static BluetoothInstructStackUtil instance;

    private BluetoothInstructStackUtil() {
        super();
        instructQueue = new Stack<>();
//        startQueueLooper();

        mInstructLooper = new InstructLooper("mInstructLooper");

        mInstructLooper.start();
    }

    public static BluetoothInstructStackUtil getInstance() {

        synchronized (BluetoothInstructStackUtil.class) {
            if (null == instance) {
                instance = new BluetoothInstructStackUtil();
            }
        }

        return instance;
    }

    //将指令添加到指令栈中
    public static void addInstructToQueue(String instruct) {

        instructQueue.push(instruct);
        Log.e(TAG, "addInstructToQueue: "+ Arrays.toString(instructQueue.toArray()));

        synchronized (instructQueue) {
            instructQueue.notify();
        }

    }

    //获取指令栈中的第一条指令
    private String getInstructFromQueue() {
        return instructQueue.pop();
    }

    public static void stopInstructLooper() {
        Log.e(TAG, "run: ----------------stopInstructLooper" + "---------" + DateFormatUtil.Long2StandardTime(System.currentTimeMillis()));
        if (queueLooperState) {
            queueLooperState = !queueLooperState;
        }
    }

    /**
     * 设置蓝牙循环到指令后的 处理指令监听器
     *
     * @param bleInstructQueueLooperListener 需要重写的蓝牙指令循环监听器
     */
    public void setInstructQueueLooperListener(BleInstructQueueLooperListener bleInstructQueueLooperListener) {
        this.mbleInstructQueueLooperListener = bleInstructQueueLooperListener;
    }

    class InstructLooper extends Thread{
        public InstructLooper(String threadName){
            super(threadName);
        }

        public void run(){
            synchronized (instructQueue) {
                while (queueLooperState) {
                    if (instructQueue.size() > 0) {

                        String instruct = getInstructFromQueue();
//                    if (null == instruct || "".equals(instruct)) {
//                        Log.e(TAG, "run: 当前蓝牙指令为空");
//                    }
                        mbleInstructQueueLooperListener.handleCurrentBleInstruct(instruct);
                    }

                    try {
                        instructQueue.wait();
                    } catch (InterruptedException e) {
                        Log.e(TAG, "run: instructLooper子线程休眠异常");
                        e.printStackTrace();
                    }
                }
            }
        }
    }

}
